/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.MenuTreeDao;
import org.istt.domain.Menu;
import org.istt.domain.MenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author NDTHO
 *
 */
@Repository("menuTreeDao")
public class MenuTreeDaoHibernate extends BaseDaoHibernate<MenuTree, Long> implements MenuTreeDao {
	Log logger = LogFactory.getLog(MenuTreeDaoHibernate.class);
	@Autowired
	public MenuTreeDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	public void saveMenuTree(MenuTree menuTree) {
		
		if(!checkDuplicate(menuTree)){
			List<MenuTree> lstSubMenu = getListMenuTree(menuTree.getChildMenuId());
			if(lstSubMenu.size()!=0){
				logger.debug("set of submenu: " + lstSubMenu.size());
				removeEntities(lstSubMenu);
			}
			makePersistent(menuTree);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("menuId set to: " + menuTree.getId());
		}
	}
	
	/***
	 * Check duplicate
	 * @param menuTree
	 * @return
	 */
	private boolean checkDuplicate(final MenuTree menuTree){
		MenuTree menu =  (MenuTree) hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(MenuTree.class);
						criteria.add(Restrictions.eq("childMenuId", menuTree.getChildMenuId()));
						criteria.add(Restrictions.eq("menu", menuTree.getMenu()));
						return criteria.uniqueResult();
			}
		});
		
		if(menu!=null) return true;
		
		return false;
	}

	@Override
	public void removeEntityByChildMenu(Long menuId) {
		
		MenuTree menuTreeEntity = getMenuTreeByMenuId(menuId);
		if(menuTreeEntity!=null)
			makeTransient(menuTreeEntity);
	}
	
	public MenuTree getMenuTreeByMenuId(final Long menuId) {
		
		MenuTree menu =  (MenuTree) hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(MenuTree.class);
						criteria.add(Restrictions.eq("childMenuId", menuId));
						return criteria.uniqueResult();
			}
		});
        return menu;
	}
	
	public Menu getFatherMenu(final Long childMenuId){
		Menu fatherMnu = null;
		
		MenuTree menu =  (MenuTree) hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(MenuTree.class);
						criteria.add(Restrictions.eq("childMenuId", childMenuId));
						return criteria.uniqueResult();
			}
		});
		
		if(menu!=null) fatherMnu = menu.getMenu();
		
		return fatherMnu;
	}
	
	public List<MenuTree> getListMenuTree(final Long childMenuId){
		@SuppressWarnings("unchecked")
		List<MenuTree> lstmenu =  (List<MenuTree>)hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,	SQLException {
						Criteria criteria = session.createCriteria(MenuTree.class);
						criteria.add(Restrictions.eq("childMenuId", childMenuId));
						return criteria.list();
			}
		});
        return lstmenu;
	}

}
